﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using RetailManagement.Models;

namespace RetailManagement.Controllers
{
    public class OrderListController : Controller
    {
        public static int QuantityOrder;
        public static int ProductIDOrder;
        public static int CustomerID;
        RetailManagementEntities re = new RetailManagementEntities();
        //
        // GET: /Order/

        public ActionResult OrdersCart(int id)
        {
            ProductIDOrder = id;
            string[] NumberRange = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
            SelectList list = new SelectList(NumberRange);
            ViewBag.myList = list;
            var q = from o in re.list_voucher where o.Product_ID == id select o;
            return View(q);
        }

        [HttpPost]
        public ActionResult OrdersCart(string quantity)
        {
            QuantityOrder = Convert.ToInt32(quantity);
                return RedirectToAction("Details");
        }

        public ActionResult Details()
        {
            try
            {

                string cusEmail = HttpContext.User.Identity.Name;
                int proID = ProductIDOrder;
                ViewBag.QuantityOrder = QuantityOrder;
                var q = from o in re.Customers where o.Email == cusEmail select o;
                CustomerID = q.First().Customer_ID;
                ViewBag.FullName = q.First().Customer_FullName;
                ViewBag.Email = cusEmail;
                ViewBag.Address = q.First().Address;
                var p = from i in re.Products where i.Product_ID == proID select i;
                ViewBag.ProName = p.Single().Fullname;
                ViewBag.Price = p.Single().Price;
                decimal? total = p.Single().Price * QuantityOrder;
                ViewBag.total = total;
                return View();
            }
            catch
            {
                return RedirectToAction("Login", "Customer");
            }
        }
        [HttpPost]
        public ActionResult Details(OrderDB ord)
        {
            try
            {
                Order db = new Order();
                db.Comment = ord.Comment;
                db.Customer_ID = CustomerID;
                db.Product_ID = ProductIDOrder;
                db.Quantity = QuantityOrder;
                db.Date_Buy = DateTime.Now;
                db.State = "Hoàn thành";

                re.AddToOrders(db);
                re.SaveChanges();

                return RedirectToAction("ThankYou");
            }
            catch
            {
                return PartialView("Error");
            }
            
        }

        public ViewResult ThankYou()
        {
            return View();
        }
    }
}
